package apps.web.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UsernameNotFoundException;

import webframe.common.helper.FormatTransformHelper;
import apps.web.model.EntUser;

public class EntUserService extends UserService {
	private static Log log = LogFactory.getLog(EntUserService.class);

	@Override
	// /////////implemented interface
	public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, DataAccessException {
		String username = FormatTransformHelper.transEncodingFormBrowser(userName, "utf-8");
		EntUser user = findEntUserByUsername(username);
		if (user == null) {
			log.info("用户名或密码错误 username=\"" + username + "\"");
			RuntimeException ex = new UsernameNotFoundException("用户名或密码错误");
			throw ex;
		}
		getEntUserDao().getHibernateTemplate().getSessionFactory().getCurrentSession().evict(user);
		List<GrantedAuthority> gas = new ArrayList<GrantedAuthority>();
		gas.add(new GrantedAuthorityImpl("ROLE_USER"));
		user.setAuthorities(gas.toArray(new GrantedAuthority[] {}));
		return user;
	}

}
